package com.huitone.gddw.service.impl.worktable;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.huitone.gddw.common.ComConstant;
import com.huitone.gddw.dao.entity.PriRuleDef;
import com.huitone.gddw.dao.worktable.paramconfigmanage.IPriRuleDefDao;
import com.huitone.gddw.model.support.QueryPage;
import com.huitone.gddw.service.IParamtabRecService;
import com.huitone.gddw.service.IPriRuleDefService;
import com.huitone.gddw.service.dto.PriRuleDefDTO;
import com.huitone.gddw.utils.SysLoginUtil;

/**
 * 优先级规则 接口实现类
 * @author wuluodan
 *
 */
@Service
public class PriRuleDefService implements IPriRuleDefService {

	@Autowired
	private IPriRuleDefDao priRuleDefDao;
	
	@Autowired
	private IParamtabRecService paramtabRecService;
	
	@Override
	public QueryPage<PriRuleDefDTO> selectPage(QueryPage<PriRuleDef> page) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("ruleId", page.getParams().get("ruleId"));
		map.put("start", (page.getPageNo() - 1) * page.getPageSize() + 1);
		map.put("end", page.getPageNo() * page.getPageSize());
		List<PriRuleDefDTO> result = priRuleDefDao.selectPage(map);
		int totalCount = priRuleDefDao.selectCount(map);
		
		QueryPage<PriRuleDefDTO> queryPage = new QueryPage<PriRuleDefDTO>();
		queryPage.setResult(result);
		queryPage.setPageNo(page.getPageNo());
		queryPage.setPageSize(page.getPageSize());
		queryPage.setTotalCount(totalCount);
		return queryPage;
	}
	
	@Override
	public List<PriRuleDef> getAll() {
		return priRuleDefDao.getAll();
	}
	
	@Override
	public PriRuleDef selectByPrimaryKey(String ruleId) {
		return priRuleDefDao.selectByPrimaryKey(ruleId);
	}

	@Override
	public PriRuleDefDTO toUpdate(String ruleId) {
		return new PriRuleDefDTO(this.selectByPrimaryKey(ruleId));
	}

	@Override
	public void changeStatus(PriRuleDef entity) {
		this.update(entity);
	}

	@Override
	public void save(PriRuleDef entity) {
		entity.setInsertTime(new Date());
		entity.setInsertStaffId(SysLoginUtil.getLoginUserId());
		entity.setLastModTime(entity.getInsertTime());
		entity.setLastModStaffId(entity.getInsertStaffId());
		priRuleDefDao.insertSelective(entity);
		
		this.updateParamRec("保存优先级规则");
	}

	@Override
	public void update(PriRuleDef entity) {
		entity.setLastModTime(new Date());
		entity.setLastModStaffId(SysLoginUtil.getLoginUserId());
		priRuleDefDao.updateByPrimaryKey(entity);
		
		this.updateParamRec("更新优先级规则");
	}

	@Override
	public void delete(String ruleId) {
		priRuleDefDao.deleteByPrimaryKey(ruleId);
		this.updateParamRec("删除优先级规则");
	}
	
	/**
	 * 更新参数记录表
	 * @param desc 说明
	 */
	private void updateParamRec(String desc) {
		paramtabRecService.update(ComConstant.PRI_RULE_DEF, desc);
	}

	@Override
	public List<PriRuleDef> getByLevel(Integer level) {
		return priRuleDefDao.getByLevel(level);
	}

}
